package com.llkj.contractmanagement.contractmanagement.contract;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.customview.ShareDialog;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.model.ContractListBean;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.LogUtil;

import java.util.HashMap;

import butterknife.Bind;

/**
 * author: dong.yj
 * created on: 2016/8/22 12:31
 * description:合同详情
 */
public class ContractDetailsActivity extends BaseActivity {
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_contract_details;
    }

    @Bind(R.id.title_contract_details)
    TitleBar titleBar;
    @Bind(R.id.webview_contract_details)
    WebView webView;
    private ShareDialog shareDialog;
    private UserInfoBean userInfoBean;
    private String ht_id;
    private ContractListBean.HtlistBean htlistBean;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();
    }

    private void init() {
        htlistBean = (ContractListBean.HtlistBean) getIntent().getSerializableExtra("contractDetails");
        userInfoBean = UserInfoBean.getInstance();
        ht_id = getIntent().getStringExtra("ht_id");
        initWebview();
        titleBar.setTopBarClickListener(this);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dismissWaitDialog();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showWaitDialog();
            }
        });
        requestAddHtZj();
    }

    private void requestAddHtZj() {
        HashMap<String, String> map = new HashMap<>();
//        user_id	1	string	用户ID
//        ht_id	1	string	合同id
        map.put("user_id", userInfoBean.getId());
        map.put("ht_id", htlistBean.getHt_id());
        HttpMethod.htZj(this, this, map, UrlConfig.HTTP_HT_ZJ);

    }

    private void initWebview() {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);// 设定支持缩放
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setDomStorageEnabled(true);
        webView.requestFocus();
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        LogUtil.e("url---------" + htlistBean.getUrl());
        webView.loadUrl(htlistBean.getUrl());
    }

    @Override
    public void rightClick() {
        super.rightClick();
        if (shareDialog == null) {
            shareDialog = new ShareDialog(this);
        }
        shareDialog.show();
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_HT_ZJ:

                break;
        }
    }
}
